import request from '@/utils/request'
import type { ApiResponse, ApiListResponse } from '@/types/common'

// 诉讼订单接口类型定义
export interface LawsuitOrder {
  id?: number
  order_id: number
  customer_id: number
  is_first_order: 0 | 1
  order_type: 1 | 2
  entrusting_party_type: 1 | 2
  entrusting_party_name: string
  opposing_party_type: 1 | 2
  opposing_party_name: string
  contact_person: string
  contact_phone?: string
  service_fee_pre: number
  service_fee_post: number
  case_type: number[]
  case_type_other?: string
  civil_phase: string[]
  criminal_phase: string[]
  case_amount_range: number
  case_amount_other?: string
  lawsuit_request_elements: string[]
  main_lawsuit_request?: string
  business_manager?: string
  department_manager?: string
  contract_manager?: string
  finance_manager?: string
  lawsuit_manager?: string
  lead_lawyer?: string
  create_time?: string
  update_time?: string
  // 关联数据
  order?: any
  customer?: any
}

// 法务服务订单接口类型定义
export interface LegalServiceOrder {
  id?: number
  order_id: number
  customer_id: number
  customer_name: string
  contact_person: string
  contact_phone: string
  service_fee: number
  package_type: 1 | 2 | 3
  account_type: 1 | 2
  sub_account_owner?: string
  is_first_order_service: 0 | 1
  lawsuit_guidance_count: number
  debt_collection_count: number
  legal_letter_count: number
  contract_draft_count: number
  contract_review_count: number
  special_service_types: string[]
  business_manager?: string
  department_manager?: string
  contract_manager?: string
  finance_manager?: string
  lawsuit_manager?: string
  lead_lawyer?: string
  create_time?: string
  update_time?: string
  // 关联数据
  order?: any
  customer?: any
}

// 诉讼订单查询参数
export interface LawsuitOrderQuery {
  keyword?: string
  status?: string
  page?: number
  limit?: number
}

// 法务订单查询参数
export interface LegalOrderQuery {
  keyword?: string
  status?: string
  page?: number
  limit?: number
}

/**
 * 诉讼和法务订单API
 */
export const lawsuitLegalOrderApi = {
  // 通用列表方法
  list: (params: {
    type: 'lawsuit' | 'legal'
    page?: number
    limit?: number
    keyword?: string
    status?: string
    customer_id?: string
    order_type?: string
  }): Promise<ApiListResponse<any>> => {
    return request.get(
      params.type === 'lawsuit'
        ? '/api/crm/lawsuit-legal-orders/lawsuit-list'
        : '/api/crm/lawsuit-legal-orders/legal-list',
      { params }
    )
  },

  // 诉讼订单相关接口

  /**
   * 获取诉讼订单列表
   */
  getLawsuitOrderList: (query: LawsuitOrderQuery = {}): Promise<ApiListResponse<LawsuitOrder>> => {
    return request.get('/api/crm/lawsuit-legal-orders/lawsuit-list', { params: query })
  },

  /**
   * 获取诉讼订单详情
   */
  getLawsuitOrderDetail: (id: number): Promise<ApiResponse<LawsuitOrder>> => {
    return request.get(`/api/crm/lawsuit-legal-orders/lawsuit-detail/${id}`)
  },

  /**
   * 创建或更新诉讼订单
   */
  saveLawsuitOrder: (data: Partial<LawsuitOrder>): Promise<ApiResponse<void>> => {
    return request.post('/api/crm/lawsuit-legal-orders/save-lawsuit-order', data)
  },

  /**
   * 删除诉讼订单
   */
  deleteLawsuitOrder: (id: number): Promise<ApiResponse<void>> => {
    return request.delete(`/api/crm/lawsuit-legal-orders/delete-lawsuit-order/${id}`)
  },

  // 法务订单相关接口

  /**
   * 获取法务订单列表
   */
  getLegalOrderList: (query: LegalOrderQuery = {}): Promise<ApiListResponse<LegalServiceOrder>> => {
    return request.get('/api/crm/lawsuit-legal-orders/legal-list', { params: query })
  },

  /**
   * 获取法务订单详情
   */
  getLegalOrderDetail: (id: number): Promise<ApiResponse<LegalServiceOrder>> => {
    return request.get(`/api/crm/lawsuit-legal-orders/legal-detail/${id}`)
  },

  /**
   * 创建或更新法务订单
   */
  saveLegalOrder: (data: Partial<LegalServiceOrder>): Promise<ApiResponse<void>> => {
    return request.post('/api/crm/lawsuit-legal-orders/save-legal-order', data)
  },

  /**
   * 创建诉讼订单
   */
  create: (
    data: Partial<LawsuitOrder> & { type: 'lawsuit' | 'legal' }
  ): Promise<ApiResponse<void>> => {
    if (data.type === 'lawsuit') {
      return request.post('/api/crm/lawsuit-legal-orders/save-lawsuit-order', data)
    } else {
      return request.post('/api/crm/lawsuit-legal-orders/save-legal-order', data)
    }
  },

  /**
   * 删除法务订单
   */
  deleteLegalOrder: (id: number): Promise<ApiResponse<void>> => {
    return request.delete(`/api/crm/lawsuit-legal-orders/delete-legal-order/${id}`)
  },
}
